/**
 * File:   custom_cmds.c
 * Author: AWTK Develop Team
 * Brief:  custom_cmds
 *
 * Copyright (c) 2023 - 2024  Guangzhou ZHIYUAN Electronics Co.,Ltd.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * License file for more details.
 *
 */

/**
 * History:
 * ================================================================
 * 2024-03-12 Li XianJing <xianjimli@hotmail.com> created
 *
 */

#include "conf_io/conf_node.h"
#include "hmi/hmi_model.h"
#include "hmi/hmi_service.h"

#define PROP_TEMP "温度"

static ret_t inc_temp_exec(hmi_model_cmd_t* cmd, tk_object_t* obj, const char* args) {
  int temp = tk_object_get_prop_int(obj, PROP_TEMP, 0);
  tk_object_set_prop_int(obj, PROP_TEMP, temp + 1);

  return RET_OBJECT_CHANGED;
}

static bool_t inc_temp_can_exec(hmi_model_cmd_t* cmd, tk_object_t* obj, const char* args) {
  int temp = tk_object_get_prop_int(obj, PROP_TEMP, 0);
  return temp < 100;
}

static const hmi_model_cmd_t s_inc_temp_cmd = { 
    .name = "inc_temp",
    .exec = inc_temp_exec,
    .can_exec = inc_temp_can_exec,
};

static ret_t dec_temp_exec(hmi_model_cmd_t* cmd, tk_object_t* obj, const char* args) {
  int temp = tk_object_get_prop_int(obj, PROP_TEMP, 0);
  tk_object_set_prop_int(obj, PROP_TEMP, temp - 1);

  return RET_OBJECT_CHANGED;
}

static bool_t dec_temp_can_exec(hmi_model_cmd_t* cmd, tk_object_t* obj, const char* args) {
  int temp = tk_object_get_prop_int(obj, PROP_TEMP, 0);
  return temp > 0;
}

static const hmi_model_cmd_t s_dec_temp_cmd = { 
    .name = "dec_temp",
    .exec = dec_temp_exec,
    .can_exec = dec_temp_can_exec,
};

ret_t custom_cmds_init(void) {
  tk_object_t* model = hmi_service_get_default_model();
  hmi_model_add_cmd(model, &s_inc_temp_cmd);
  hmi_model_add_cmd(model, &s_dec_temp_cmd);

  return RET_OK;
}
